/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "iot_cloud.h"
#include "iot_profile.h"
#include "iot_boardled.h"
#include "iot_boardbutton.h"
#include "iot_nfc.h"
#include "iot_wifi.h"
#include "iot_netcfg.h"
#include "iot_sntp.h"
#include "ohos_init.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <cmsis_os2.h>
#include <stdbool.h>
#include "kv_store.h"
#include "iot_schedule.h"
#include "iot_demo_def.h"
#include "aquaponics.h"
#include "SGP30.h"
#include "hi_reset.h"
#include "network_config_service.h"
#include "iot_netcfg_nan.h"

#define SID_KEY "ssid"
#define PWD_KEY "password"

#define SOFTAP_NAME "teamX-Aquaponics01"

// DEFAULT PARAM, AND MUST BE MODIFIED TO THE REAL PARAMS
#define CONFIG_CLOUD_DEFAULT_SERVERIP "121.36.42.100" // replace it with iotplatform server ip
#define CONFIG_CLOUD_DEFAULT_SERVERPORT "1883"        // replace it with iotplatform server  port
#define CONFIG_SENSOR_SAMPLE_CYCLE 10000              // unit:minisecond
#define CONFIG_TASK_DEFAULT_STACKSIZE 0x1000          // unit:bytes
#define CONFIG_TASK_DEFAULT_PRIOR 20                  // default task priority
#define CONFIG_TASK_MAIN_STACKSIZE 0x4000             // main task stacksize must be bigger
#define CONFIG_TASK_MAIN_PRIOR 25                     // default task priority
#define CONFIG_LOCAL_TIMEZONE 8
#define CONFIG_FLASHLED_FRENETCONFIG 8
#define CONFIG_FLASHLED_FREWIFI 4
#define CONFIG_FLASHLED_FRECLOUD 1
#define CONFIG_FLASHLED_WORKSWELL 0.1
#define MAIN_TASK_DELAY_TICKS 100
#define CONFIG_SENSOR_SAMPLE_CYCLE 6000

typedef struct
{
    int curDay;
    int curSecondsInDay;
    bool lightManual;
} ApplicationController;
static ApplicationController g_appController;

static void RtcTimeUpdate(void)
{
    extern int SntpGetRtcTime(int localTimeZone, struct tm *rtcTime);
    struct tm rtcTime;
    SntpGetRtcTime(CONFIG_LOCAL_TIMEZONE, &rtcTime);

    RaiseLog(LOG_LEVEL_INFO, "Year:%d Month:%d Mday:%d Wday:%d Hour:%d Min:%d Sec:%d",
             rtcTime.tm_year + BASE_YEAR_OF_TIME_CALC, rtcTime.tm_mon + 1, rtcTime.tm_mday,
             rtcTime.tm_wday, rtcTime.tm_hour, rtcTime.tm_min, rtcTime.tm_sec);

    if (rtcTime.tm_wday > 0)
    {
        g_appController.curDay = rtcTime.tm_wday - 1;
    }
    else
    {
        g_appController.curDay = EN_SUNDAY;
    }

    g_appController.curSecondsInDay = rtcTime.tm_hour * CN_SECONDS_IN_HOUR +
                                      rtcTime.tm_min * CN_SECONDS_IN_MINUTE + rtcTime.tm_sec + 8; // add 8 s offset
}

/**
 * @brief Convert miniseconds to system ticks
 * @param ms Indicates the mimiseconds to convert
 * @return Returns the corresponding ticks of specified time
 */
static uint32_t Time2Tick(uint32_t ms)
{
    uint64_t ret;
    ret = ((uint64_t)ms * osKernelGetTickFreq()) / CN_MINISECONDS_IN_SECOND;
    return (uint32_t)ret;
}

/**
 * @brief Sensor data collection task entry and the main flow is as follows:
 *        1, Wait the for the event to trigger in specified time
 *        2, Collect Sensor Data (air humidity/temperature, soil TDS, etc.)
 *        3, Report the collected data to the Cloud Platform
 */
static void DataCollectAndReport(const void *arg)
{
    (void)arg;
    uint32_t waitTicks;
    int temperature = 0;
    int humidity = 0;
    int lux = 0;
    float CO2;
    int TDS = 0;
    short watertemp = 0;
    float PH = 0;
    waitTicks = Time2Tick(CONFIG_SENSOR_SAMPLE_CYCLE);
    while (1)
    {
        if (AquaponicsGetAirStatus(&temperature, &humidity, &lux) != 0)
        {
            RaiseLog(LOG_LEVEL_ERR, "AquaponicsGetAirStatus temperature && humidity && lux failed! \n");
        }
        if (SGP30_GetC02(&CO2) != 0)
        {
            RaiseLog(LOG_LEVEL_ERR, "SGP30_GetC02 failed! \n");
        }
        if (DS18B20_Get_Temp(&watertemp) != 0)
        {
            RaiseLog(LOG_LEVEL_ERR, "DS18B20_Get_Temp failed! \n");
        }
        if (AquaponicsGetTDSStatus(&TDS) != 0)
        {
            RaiseLog(LOG_LEVEL_ERR, "AquaponicsGetTDSStatus failed! \n");
        }
        if (AquaponicsGetPHStatus(&PH) != 0)
        {
            RaiseLog(LOG_LEVEL_ERR, "AquaponicsGetPHStatus failed! \n");
        }

        IotProfile_Report(temperature, humidity, lux, CO2, watertemp / 10, TDS, PH);
        RaiseLog(LOG_LEVEL_INFO, "Get the sensor Data-->temperature:%d humidity:%d lux:%d CO2:%.2f watertemp:%d.%d TDS:%d PH:%.2f",
                 temperature, humidity, lux, CO2, watertemp / 10, watertemp % 10, TDS, PH);
        osDelay(Time2Tick(CONFIG_SENSOR_SAMPLE_CYCLE));
    }
    return;
}

static int IotWifiInfo_get(char *ssid, int id_size, char *pwd, int pd_size)
{
    int retval = UtilsGetValue(SID_KEY, ssid, id_size);
    if (retval <= 0)
    {
        RaiseLog(LOG_LEVEL_ERR, "no such ssid stored! \n");
        return 0;
    }

    if (UtilsGetValue(PWD_KEY, pwd, pd_size) < 0)
    {
        RaiseLog(LOG_LEVEL_INFO, "ssid(%s) no password stored! \n", ssid);
    }
    else
    {
        RaiseLog(LOG_LEVEL_INFO, "ssid : %s, pwd : %s! \n", ssid, pwd);
    }

    return 1;
}

static void IotWifiInfo_set(char *ssid, char *pwd)
{
    if (UtilsSetValue(SID_KEY, ssid) != 0)
    {
        RaiseLog(LOG_LEVEL_ERR, "store ssid failed! \n");
        return;
    }
    if (UtilsSetValue(PWD_KEY, pwd) != 0)
    {
        RaiseLog(LOG_LEVEL_ERR, "store password failed! \n");
        UtilsDeleteValue(SID_KEY);
        return;
    }
    RaiseLog(LOG_LEVEL_INFO, "store password success! \n");
}

/**
 * @brief IoTMain task entry and the main flow is as follows:
 *        1, Initialize the necessary hardware and obtain the corresponding information from NFC
 *        2, Connecting to wifi
 *        3, Connecting to the HUAWEI CLOUD Platform
 *        4, Creating a Sensor Collection Task
 */
static void IotMainTaskEntry(const void *arg)
{
    osThreadAttr_t attr;
    NfcInfo nfcInfo;
    (void)arg;
    char ssid[BUFF_SIZE] = {0};
    char pwd[BUFF_SIZE] = {0};
    int ret = 0;

    BOARD_InitPwmLed();
    AquaponicsInit();
    SGP30_Init();
    BOARD_InitWifi();

    IOT_ScheduleInit();
    ret = Board_IsButtonPressedF2();
    osDelay(MAIN_TASK_DELAY_TICKS);

    // if any netconfiguration
    LedFlashFrequencySet(CONFIG_FLASHLED_FRENETCONFIG);

    // set it for the debug
    nfcInfo.deviceID = "62c6ee2e6b9813541d5048fa_Aquaponics01";
    nfcInfo.devicePWD = "12345678";

    if (ret)
    {
        RaiseLog(LOG_LEVEL_INFO, "Netconfig Button has pressed! \n");
        if (BOARD_NAN_NetCfgStartConfig(SOFTAP_NAME, ssid, sizeof(ssid), pwd, sizeof(pwd)) < 0)
        {
            RaiseLog(LOG_LEVEL_ERR, "BOARD_NetCfgStartConfig failed! \n");
            return;
        }
        else
        {
            ret = AFTER_NETCFG_ACTION;
        }
    }
    else
    {
        ret = IotWifiInfo_get(ssid, sizeof(ssid), pwd, sizeof(pwd));
        if (ret == 0)
        {
            if (BOARD_NAN_NetCfgStartConfig(SOFTAP_NAME, ssid, sizeof(ssid), pwd, sizeof(pwd)) < 0)
            {
                RaiseLog(LOG_LEVEL_ERR, "BOARD_NetCfgStartConfig failed! \n");
                return;
            }
            else
            {
                ret = AFTER_NETCFG_ACTION;
            }
        }
    }

    LedFlashFrequencySet(CONFIG_FLASHLED_FREWIFI);
    if (BOARD_ConnectWifi(ssid, pwd) != 0)
    {
        RaiseLog(LOG_LEVEL_ERR, "BOARD_ConnectWifi failed! \n");
        if (ret == AFTER_NETCFG_ACTION)
        {
            NotifyNetCfgResult(NETCFG_DEV_INFO_INVALID);
        }
        hi_hard_reboot(HI_SYS_REBOOT_CAUSE_CMD);
        return;
    }

    if (ret == AFTER_NETCFG_ACTION)
    {
        RaiseLog(LOG_LEVEL_DEBUG, "Connect wifi success ! \n");
        NotifyNetCfgResult(NETCFG_OK);
        osDelay(MAIN_TASK_DELAY_TICKS);
        RaiseLog(LOG_LEVEL_DEBUG, "StopNetCfg wifi success ! \n");
        StopNetCfg();
        IotWifiInfo_set(ssid, pwd);
    }
    // update the RTC time and connect to the cloud
    LedFlashFrequencySet(CONFIG_FLASHLED_FRECLOUD);
    RtcTimeUpdate();
    if (CLOUD_Init() != 0)
    {
        return;
    }

    if (CLOUD_Connect(nfcInfo.deviceID, nfcInfo.devicePWD,
                      CONFIG_CLOUD_DEFAULT_SERVERIP, CONFIG_CLOUD_DEFAULT_SERVERPORT) != 0)
    {
        return;
    }

    LedFlashFrequencySet(CONFIG_FLASHLED_WORKSWELL);

    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = CONFIG_TASK_DEFAULT_STACKSIZE;
    attr.priority = CONFIG_TASK_DEFAULT_PRIOR;
    attr.name = "DataCollectAndReport";
    if (osThreadNew((osThreadFunc_t)DataCollectAndReport, NULL, (const osThreadAttr_t *)&attr) == NULL)
    {
        return;
    }
    return;
}

/**
 * @brief IoT Main Entry of the IoT-Aquaponics project
 */
static void IotMainEntry(void)
{
    osThreadAttr_t attr;
    RaiseLog(LOG_LEVEL_INFO, "DATA:%s Time:%s \r\n", __FUNCTION__, __DATE__, __TIME__);
    memset(&g_appController, 0x00, sizeof(g_appController));

    // Create the IoT Main task
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = CONFIG_TASK_MAIN_STACKSIZE;
    attr.priority = CONFIG_TASK_MAIN_PRIOR;
    attr.name = "IoTMain";
    (void)osThreadNew((osThreadFunc_t)IotMainTaskEntry, NULL, (const osThreadAttr_t *)&attr);

    return;
}
// Add the IotMainEntry function to the System Startup Procedure
APP_FEATURE_INIT(IotMainEntry);
